home *** CD-ROM | disk | FTP | other *** search
/ HAKERIS 11 / HAKERIS 11.ISO / soft / development / Macromedia RoboHelp X5 / RoboHelpOffice.exe / Data1.cab / _0907ACAF8C9F4E45B1B703DA1482AEAA < prev    next >
Encoding:
Text File  |  2003-02-08  |  11.9 KB  |  374 lines

  1. // eHelp« Corporation
  2. // Copyright⌐ 1998-2001 eHelp« Corporation.All rights reserved.
  3. // RoboHelp_CSH.java
  4. // The Helper class for RoboInfo and WebHelp Context Sensitive Help
  5.  
  6. /* Example function calls
  7. // First, create RH_WindowOption object
  8. RoboHelp_CSH.RH_WindowOption MyWin = new RoboHelp_CSH.RH_WindowOption();
  9. // Set window style and position in object
  10. MyWin.m_nStyle = RoboHelp_CSH.RHWO_LOCATION | RoboHelp_CSH.RHWO_MENUBAR | RoboHelp_CSH.RHWO_RESIZABLE;
  11. MyWin.m_nTop = 50;
  12. MyWin.m_nLeft=50;
  13. MyWin.m_nHeight=300;
  14. MyWin.m_nWidth=400;
  15.  
  16. // Show Help for topic with HTML Help Map number 11:
  17. RoboHelp_CSH.RH_ShowHelpById("c:\\myapp\\help\\start_rhc.htm", "11", "myHelpWindow", MyWin)
  18.  
  19. // Show Help for topic with Context String ID "My_Topic"
  20. RoboHelp_CSH.RH_ShowHelpByString("c:\\myapp\\help\\start_rhc.htm", "My_Topic", "myHelpWindow", MyWin)
  21. */
  22.  
  23. import netscape.javascript.*;
  24. import java.applet.Applet;
  25. import java.net.*;
  26. import java.io.File;
  27.  
  28.  
  29. public class RoboHelp_CSH
  30. {
  31.     public static class RH_WindowOption {
  32.         public RH_WindowOption() {};
  33.         public int m_nStyle;        /* see above RHWO_XXX  */
  34.         public int m_nTop;            /* top position (Y) */
  35.         public int m_nLeft;        /* left position (X) */
  36.         public int m_nHeight;        /* window's height */
  37.         public int m_nWidth;        /* window's width */
  38.     };
  39.     public static  final int RHWO_LOCATION        = 0x1;
  40.     public static  final int RHWO_MENUBAR        = 0x2;
  41.     public static  final int RHWO_RESIZABLE        = 0x4;
  42.     public static  final int RHWO_TOOLBAR        = 0x8;
  43.     public static  final int RHWO_STATUS        = 0x10;
  44.     public static  final int RHWO_SCROLLBARS    = 0x20;
  45.     private static final String AUTOCONTEXTID_PREFIX = "HelpIdFromHTMLHelp";
  46.     
  47.     /**********************************************
  48.      Show Help topic by topic number generated for HtmlHelp.
  49.      Parameters:
  50.       [in] strUrlToHelpSet:  url to helpset. for WebHelp Enterprise it is the url to server name, such as "http://helpserver.com/RoboApi.asp"
  51.                                for webhelp, it is full path name the webhelp start page name, such as "c:/myapp/help/start_rhc.htm" or "/user/myapp/help/start_rhc.htm"
  52.       [in] nTopicNumber:     topic number generated for the HTMLHelp context sensitive help.
  53.       [in] strTargetId:      browser's target name, such as "mynewtopic"
  54.       [in] cWindowOption:   browser options. can be 0.
  55.      Result:
  56.        none.
  57.     **********************************************/
  58.  
  59.     public static boolean RH_ShowHelpById(String strUrlToHelpSet, int nTopicNumber, String strTargetId, RH_WindowOption cWindowOption, Applet applet)
  60.     {
  61.         String strContextID = AUTOCONTEXTID_PREFIX + "_" + nTopicNumber;
  62.         return RH_ShowHelpByString(strUrlToHelpSet, strContextID, strTargetId, cWindowOption, applet);
  63.     }
  64.  
  65.     /**********************************************
  66.      Show Help topic
  67.      Parameters:
  68.       [in] strUrlToHelpSet:  url to helpset. for WebHelp Enterprise it is the url to server name, such as "http://helpserver.com/RoboApi.asp"
  69.                                for webhelp, it is full path name the webhelp start page name, such as "c:/myapp/help/start_rhc.htm" or "/user/myapp/help/start_rhc.htm"
  70.       [in] strContextID:     context id of the topic
  71.       [in] strTargetId:      browser's target name, such as "mynewtopic"
  72.       [in] cWindowOption:   browser options. can be 0.
  73.      Result:
  74.        none.
  75.     **********************************************/
  76.     public static boolean RH_ShowHelpByString(String strUrlToHelpSet, String strContextID, String strTargetId, RH_WindowOption cWindowOption, Applet applet)
  77.     {
  78.         if (strUrlToHelpSet == null || strUrlToHelpSet.length() == 0 ) return false;
  79.         if (cWindowOption == null)
  80.         {
  81.             cWindowOption = new RH_WindowOption();
  82.             cWindowOption.m_nStyle = 0;
  83.             cWindowOption.m_nTop = 10;
  84.             cWindowOption.m_nLeft = 10;
  85.             cWindowOption.m_nHeight = 300;
  86.             cWindowOption.m_nWidth  = 400;
  87.         }
  88.  
  89.         String strURL = "";
  90.         if (strContextID == null) 
  91.             strContextID = "";
  92.  
  93.         //let different agent to make url.
  94.         if (isServerBased(strUrlToHelpSet))
  95.         {
  96.             strURL = makeUrlForServerBased(strUrlToHelpSet,strContextID);
  97.         }
  98.         else
  99.         {
  100.             strURL = makeUrlForWebHelpBased(strUrlToHelpSet,strContextID);
  101.         }
  102.         showHelpTopic(strURL, cWindowOption, strTargetId, applet);
  103.         return true;
  104.     }
  105.  
  106.     public static boolean isServerBased(String strUrlToHelpSet)
  107.     {
  108.         return (strUrlToHelpSet.toLowerCase().lastIndexOf(".asp") == 
  109.             strUrlToHelpSet.length() - 4);
  110.     }
  111.     
  112.     public static String makeUrlForServerBased(String strUrlToHelpSet, String strContextID)
  113.     {
  114.         return strUrlToHelpSet + "?context=" + strContextID;
  115.     }
  116.  
  117.     public static String makeUrlForWebHelpBased(String strUrlToHelpSet, String strContextID)
  118.     {
  119.         return strUrlToHelpSet + "#context=" + strContextID;
  120.     }
  121.  
  122.     private static  boolean showHelpTopic(String strURL,  RH_WindowOption cWindowOption, String strTargetId, Applet applet) {
  123.         String strHelpOptions = "";
  124.     
  125.         if ((cWindowOption.m_nStyle & RHWO_LOCATION) != 0)            // Show/Hide Address bar {yes | no}
  126.             strHelpOptions += ",location=yes";
  127.         else
  128.             strHelpOptions += ",location=no";
  129.  
  130.         if ((cWindowOption.m_nStyle & RHWO_TOOLBAR) != 0)            // Show/Hide Toolbar {yes | no}
  131.             strHelpOptions += ",toolbar=yes";        
  132.         else
  133.             strHelpOptions += ",toolbar=no";        
  134.  
  135.         if ((cWindowOption.m_nStyle & RHWO_MENUBAR)    != 0)        // Show/Hide Menubar {yes | no}
  136.             strHelpOptions += ",menubar=yes";        
  137.         else
  138.             strHelpOptions += ",menubar=no";
  139.  
  140.         if ((cWindowOption.m_nStyle & RHWO_STATUS) != 0)            // Show/Hide Statusbar {yes | no}
  141.             strHelpOptions += ",status=yes";        
  142.         else
  143.             strHelpOptions += ",status=no";        
  144.  
  145.         if ((cWindowOption.m_nStyle & RHWO_SCROLLBARS)!= 0)            // Show/Hide Scrollbar {yes | no}
  146.             strHelpOptions += ",scrollbars=yes";
  147.         else
  148.             strHelpOptions += ",scrollbars=no";    
  149.  
  150.         if ((cWindowOption.m_nStyle & RHWO_RESIZABLE)!= 0)                // Allow help window resizing {yes | no}
  151.             strHelpOptions += ",resizable=yes";
  152.         else
  153.             strHelpOptions += ",resizable=no";
  154.  
  155.         if (cWindowOption.m_nTop >= 0)
  156.         {
  157.             strHelpOptions += ",top=" + cWindowOption.m_nTop;        // Top position for help window (in pixels)
  158.             strHelpOptions += ",screenY=" + cWindowOption.m_nTop;        // Top position for help window (in pixels)(for netscape)
  159.         }
  160.  
  161.         if (cWindowOption.m_nLeft >= 0)
  162.         {
  163.             strHelpOptions += ",left=" + cWindowOption.m_nLeft;        // Left position for help window (in pixels)
  164.             strHelpOptions += ",screenX=" + cWindowOption.m_nLeft;    // Left position for help window (in pixels)(for netscape)
  165.         }
  166.  
  167.         if (cWindowOption.m_nWidth > 0)
  168.         {
  169.             strHelpOptions += ",width=" + cWindowOption.m_nWidth;        // Width of help window (in pixels)
  170.             strHelpOptions += ",outerWidth=" + cWindowOption.m_nWidth;    // Width of help window (in pixels) (for netscape)
  171.         }
  172.  
  173.         if (cWindowOption.m_nHeight > 0)
  174.         {
  175.             strHelpOptions += ",height=" + cWindowOption.m_nHeight;    // Height of help window (in pixels)
  176.             strHelpOptions += ",outerHeight=" + cWindowOption.m_nHeight;    // Height of help window (in pixels) (for netscape)
  177.         }
  178.         
  179.         boolean bJSAccessable = true;
  180.         boolean bSuccess = false;
  181.         if (System.getProperty("os.name").startsWith("Mac") && !System.getProperty("java.vendor").startsWith("Netscape"))
  182.             bJSAccessable = false;
  183.  
  184.         if (bJSAccessable) {
  185.             bSuccess = contextUsingJSObject(applet, strHelpOptions, strURL);
  186.         }
  187.         
  188.         if (!bJSAccessable || !bSuccess) {
  189.             try {
  190.                 String strURLWithWindowOption = strURL;
  191.                 URL url =  makeURL(applet.getDocumentBase(), strURL, strURL);
  192.                 applet.getAppletContext().showDocument(url, "HelpStub");
  193.                 return true;
  194.             }
  195.             catch (Exception e) {
  196.                 e.printStackTrace();
  197.             }
  198.         }
  199.         return false;
  200.     }
  201.  
  202.     private static boolean contextUsingJSObject(Applet applet, String strWindowOption, String strURLWithTopic)
  203.     {
  204.         try {
  205.             URL url =  makeURL(applet.getDocumentBase(),strURLWithTopic, strURLWithTopic);
  206.             JSObject win = JSObject.getWindow(applet); 
  207.             String strURLConvert = url.toString(); 
  208.             strURLConvert = strURLConvert.replace('\\', '/');
  209.             win.eval("window.open(\"" + strURLConvert + "\", \"HelpStub\", \"" + strWindowOption + "\");");
  210.             return true;
  211.         }
  212.         catch (Exception e) {
  213.             e.printStackTrace();
  214.             return false;
  215.         }
  216.     }
  217.     
  218.     // return a URL build from the local and url parameters
  219.     static URL makeURL (URL base, String local, String url) throws MalformedURLException
  220.     {
  221.         try {
  222.                 //return new URL (base, local);
  223.                 String protocol = base.getProtocol();
  224.                 String host        = base.getHost();
  225.                 String file        = base.getFile();
  226.                 int       port        = base.getPort();
  227.  
  228.                 String fileNew0 = tuHtmlToText(file);
  229.                 String fileNew1 = GetNormalizedLocal(fileNew0);
  230.                 String fileNew    = TruncURLtoQuestionMark(fileNew1);
  231.                 URL baseNew = new URL(protocol,host,port,fileNew);
  232.  
  233.                 String localNew0 = tuHtmlToText(local);
  234.                 String localNew = GetNormalizedLocal(localNew0);
  235.                 return new URL (baseNew, localNew);
  236.         }
  237.         catch (MalformedURLException x) {
  238.             x.printStackTrace();
  239.  
  240.             // no it isn't - but it might be
  241.             // a MS-type file spec (eg e:\temp\foo)
  242.             // so try opening it as a file
  243.  
  244.             File f = new File (local);
  245.             if (f.exists ()) {
  246.                 // OK - it's a local file, try appending
  247.                 // "file:/" onto it and opening it - even
  248.                 // though the slashes will be all funny
  249.                 // this seems to work!
  250.                 return new URL ("file:/" + local);
  251.             }
  252.             else {
  253.  
  254.                 // the file doesn't exist....
  255.                 // should display a dialog box here
  256.                 // and now try the secondary URL
  257.  
  258.                 return new URL (base, url);
  259.             }
  260.         }
  261.     }
  262.     
  263.     static String TruncURLtoQuestionMark(String str)
  264.     {
  265.         String strRc = str;
  266.         int nQuestionMarkPos = str.indexOf('?');
  267.         if (nQuestionMarkPos != -1) {
  268.             strRc = str.substring(0, nQuestionMarkPos);
  269.         }
  270.         return strRc;
  271.     }
  272.     
  273.     static String GetNormalizedLocal(String str)
  274.     {
  275.         String local = str;
  276.         // fix up special characters (for netscape)
  277.         for (int i = 0; i < local.length(); i++)
  278.         {
  279.             if (local.charAt(i) > 127)
  280.             {
  281.                 local = local.substring(0, i) + "%" + Integer.toString(local.charAt(i), 16) + local.substring(i + 1, local.length());
  282.             }
  283.         }
  284.         return local;
  285.     }
  286.     
  287.     //To Do: need a utility to convert html<->text
  288.     //Copy from SiteMapParserToContents
  289.     //String fixSpecialCharacters(String value) 
  290.     static String  tuHtmlToText(String value) 
  291.     {
  292.         if (value == null)
  293.             return null;
  294.         
  295.         // TODO: This should be common in SiteMapParser or something
  296.         int i = value.indexOf('&');
  297.         
  298.         if (i < 0) return value;
  299.  
  300.         String strOut = "";
  301.  
  302.         // have to convert & and the like
  303.         while (i > -1 && i < value.length() - 2) {
  304.             strOut += value.substring(0, i);
  305. //    System.out.println("1strOut is now '" + strOut + "'");
  306.             String str = value.substring(i);
  307.             String strEnd = "";
  308.             int j = str.indexOf(';');
  309.             if (j < 0) {
  310.                 strOut += str;
  311. //    System.out.println("2strOut is now '" + strOut + "'");
  312.                 break;    // no termination
  313.             }
  314.             if (j < str.length()-1) {
  315.                 value = str.substring(j+1);
  316.             }
  317.             else {
  318.                 value = "";
  319.             }
  320.             str = str.substring(1, j);
  321. //    System.out.println("value is now '" + value + "'");
  322. //    System.out.println("str is now '" + str + "'");
  323.             // TODO: Should use a hashtable and support more characters
  324.             switch (Character.toUpperCase(str.charAt(0))) 
  325.             {
  326.             case 'A':
  327.                 if (str.equalsIgnoreCase("amp")) {
  328.                     str = "&";
  329.                 }
  330.                 break;
  331.             case 'C':
  332.                 if (str.equalsIgnoreCase("copy")) {
  333.                     str = "(c)";
  334.                 }
  335.                 break;
  336.             case 'G':
  337.                 if (str.equalsIgnoreCase("gt")) {
  338.                     str = ">";
  339.                 }
  340.                 break;
  341.             case 'L':
  342.                 if (str.equalsIgnoreCase("lt")) {
  343.                     str = "<";
  344.                 }
  345.                 break;
  346.             case 'N':
  347.                 if (str.equalsIgnoreCase("nbsp")) {
  348.                     str = " ";
  349.                 }
  350.                 break;
  351.             case 'Q':
  352.                 if (str.equalsIgnoreCase("quot")) {
  353.                     str = "\"";
  354.                 }
  355.                 break;
  356.             case 'R':
  357.                 if (str.equalsIgnoreCase("reg")) {
  358.                     str = "(R)";
  359.                 }
  360.                 break;
  361.             }
  362.             strOut += str;
  363.             i = value.indexOf('&');
  364.             if (i < 0) {
  365.                 strOut += value;
  366. //    System.out.println("3strOut is now '" + strOut + "'");
  367.             }
  368.         }
  369.  
  370. //    System.out.println("4strOut is now '" + strOut + "'");
  371.         return strOut;
  372.     }
  373. }
  374.